package com.zhao.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * Spring MVC拦截器
 * 
 * @author zhaoyongfei
 *
 */
public class AppHandlerInterceptor extends HandlerInterceptorAdapter {
	
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.addHeader("Access-Control-Allow-Origin","*");
        response.addHeader("Access-Control-Allow-Methods","*");
        response.addHeader("Access-Control-Max-Age","100");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type");
        response.addHeader("Access-Control-Allow-Credentials","false");
        return super.preHandle(request, response, handler);
    }
	
	/*
	 * 
	 * 不再计算PV，UV等
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		String url = request.getRequestURL().toString();
		if (request.getMethod().equals(HttpMethod.GET.toString())) {
			if (!StringUtils.isEmpty(request.getQueryString())) {
				url = url + "?" + request.getQueryString();
			}
		} else if (request.getMethod().equals(HttpMethod.POST.toString())) {
			
		} else {
			
		}
	}
}
